package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.threadLocal.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
	@Autowired
	private FileStorageService fileStorageService;

	//上传图片
	@Override
	public ResponseResult upload(MultipartFile file) {
		try {
			//1.参数校验
			if (file == null) {
				return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "上传文件参数失效");
			}
			//2.上传图片到minio中
			//获取文件的原始名称
			String originalFilename = file.getOriginalFilename();
			String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
			String fileName = UUID.randomUUID().toString().replace("-", "") + substring;
			String path = fileStorageService.uploadImgFile("", fileName, file.getInputStream());
			//3.新增数据到wm_material表中
			WmMaterial wmMaterial = new WmMaterial();
			wmMaterial.setCreatedTime(new Date());
			wmMaterial.setUrl(path);
			wmMaterial.setType((short) 0);
			wmMaterial.setIsCollection((short) 0);
			wmMaterial.setUserId(WmThreadLocalUtil.getWmUser().getId());
			save(wmMaterial);
			//返回数据
			return ResponseResult.okResult(wmMaterial);
		} catch (IOException e) {
			e.printStackTrace();
			return ResponseResult.okResult(AppHttpCodeEnum.SERVER_ERROR);
		}
	}

	//素材列表
	@Override
	public ResponseResult list(WmMaterialDto dto) {
		//1.参数校验
		if (dto.getIsCollection() == null) {
			dto.setIsCollection((short) 0);
		}
		dto.checkParam();
		//2.分页查询
		IPage<WmMaterial> iPage = new Page<>(dto.getPage(), dto.getSize());
		LambdaQueryWrapper<WmMaterial> queryWrapper = Wrappers.lambdaQuery();
		if (dto.getIsCollection() == 1) {
			queryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
		}
		//加入用户信息查询
		queryWrapper.eq(WmMaterial::getUserId, WmThreadLocalUtil.getWmUser().getId());
		IPage<WmMaterial> resultPage = page(iPage, queryWrapper);
		//3.返回数据
		ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) resultPage.getTotal());
		result.setData(resultPage.getRecords());
		return result;
	}

	/**
	 * 删除图片
	 * @param id
	 * @return
	 */
	@Override
	public ResponseResult delete(Integer id) {
		//参数校验

		return null;
	}
}
